package com.gl.emms.whale.db;

import java.util.ArrayList;
import java.util.List;

import com.gl.emms.whale.util.ApplacationInfo;
import com.gl.emms.whale.util.EnergyEntity;
import com.gl.emms.whale.util.RechargeRecordEntity;
import com.gl.emms.whale.util.SystemMsgEntity;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class EnergyAnalysisDB {
	private SQLiteDatabase db;

	public EnergyAnalysisDB(Context context) {
		db = context.openOrCreateDatabase(ApplacationInfo.DBNAME,
				Context.MODE_PRIVATE, null);
	}

	public void saveRecord(EnergyEntity entity,String room) {
		db.execSQL("CREATE table IF NOT EXISTS Data" + room
				+ " (time TEXT PRIMARY KEY,bjsz TEXT);");

		db.execSQL("INSERT OR REPLACE into Data"+room
						+ " (time,bjsz) values(?,?)",
						new Object[] { entity.getTime().toString(),entity.getBjsz() });

	}

	public List<EnergyEntity> getRecord(String room) {
		List<EnergyEntity> list = new ArrayList<EnergyEntity>();
		db.execSQL("CREATE table IF NOT EXISTS Data"+room
				+ " (time TEXT PRIMARY KEY,bjsz TEXT);");
		Cursor c = db.rawQuery("SELECT * from Data"+room, null);
		while (c.moveToNext()) {

			String bjsz = c.getString(c.getColumnIndex("bjsz"));
			String time = c.getString(c.getColumnIndex("time"));

			//EnergyEntity entity = new EnergyEntity(bjsz, time);
			//list.add(entity);
		}
		c.close();
		return list;
	}
	public void removeRecord(int id,String room) {
		db.execSQL("CREATE table IF NOT EXISTS Data"+room
				+ " (time TEXT PRIMARY KEY,bjsz TEXT);");
		db.execSQL("DELETE from Data"+room+" where _id = ?",new Object[] { id });
	}
	public int getMaxId(String room) {
		db.execSQL("CREATE table IF NOT EXISTS Data"+room
				+ " (time TEXT PRIMARY KEY,bjsz TEXT);");
		String strSql = " Select  seq From sqlite_sequence  Where name = 'Data"+room+"'";

		Cursor mCursor = db.rawQuery(strSql, null);
		int maxId = 0;
		if(mCursor.moveToNext())
		{
			maxId = mCursor.getInt(mCursor.getColumnIndex("seq")); 
		}
		return maxId;
	}

	public void close() {
		if (db != null)
			db.close();
	}
}
